package nix.anim.cms.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nix.anim.cms.service.CmsSiteService;
import nix.anim.common.pojo.NixResult;
import nix.anim.mapper.CmsSiteMapper;
import nix.anim.pojo.CmsSite;
import nix.anim.pojo.CmsSiteExample;
import nix.anim.pojo.CmsSiteExample.Criteria;

/**
 * 站点管理Service
 */
@Service
public class CmsSiteServiceImpl implements CmsSiteService {

	@Autowired
	private CmsSiteMapper cmsSiteMapper;

	@Value("${MAX_LIST_COUNT}")
	private Integer MAX_LIST_COUNT;

	@Override
	public NixResult addCmsSite(CmsSite cmsSite) {
		// 数据验证
		// 非空验证
		if (StringUtils.isBlank(cmsSite.getDomain())) {
			return NixResult.build(400, "域名不能为空！");
		}
		// 去掉域名最后一个“/”
		if (cmsSite.getDomain().endsWith("/")) {
			cmsSite.setDomain(cmsSite.getDomain().substring(0, cmsSite.getDomain().length() - 2));
		}
		if (StringUtils.isBlank(cmsSite.getTitle())) {
			return NixResult.build(400, "站点名称不能为空！");
		}
		// 重复验证
		CmsSiteExample cmsSiteExample = new CmsSiteExample();
		Criteria criteria = cmsSiteExample.createCriteria();
		criteria.andDomainEqualTo(cmsSite.getDomain());
		List<CmsSite> cmsSiteList = cmsSiteMapper.selectByExample(cmsSiteExample);
		if (cmsSiteList != null && cmsSiteList.size() > 0) {
			return NixResult.build(400, "域名已存在！");
		}
		// 添加
		cmsSiteMapper.insert(cmsSite);
		return NixResult.ok(cmsSite);
	}

	@Override
	public NixResult getCmsSiteList(Integer page, Integer rows) {
		if (page == null || page <= 0) {
			page = 1;
		}
		if (rows == null || rows <= 0 || rows > MAX_LIST_COUNT) {
			rows = MAX_LIST_COUNT;
		}
		// 分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		List<CmsSite> list = cmsSiteMapper.selectByExample(new CmsSiteExample());
		// 取查询结果
		PageInfo<CmsSite> pageInfo = new PageInfo<>(list);
		// 返回
		return NixResult.ok(pageInfo);
	}

	@Override
	public NixResult getCmsSiteById(Integer id) {
		if (id != null) {
			CmsSite cmsSite = cmsSiteMapper.selectByPrimaryKey(id);
			if (cmsSite != null) {
				return NixResult.ok(cmsSite);
			} else {
				return NixResult.build(400, "id不存在！");
			}
		} else {
			return NixResult.build(400, "id无效！");
		}
	}

	@Override
	public NixResult updateCmsSite(CmsSite cmsSite) {
		// 数据验证
		// id验证
		NixResult result = getCmsSiteById(cmsSite.getId());
		if (result.getStatus() != 200) {
			return result;
		}
		// 非空验证
		if (StringUtils.isBlank(cmsSite.getDomain())) {
			return NixResult.build(400, "域名不能为空！");
		}
		// 去掉域名最后一个“/”
		if (cmsSite.getDomain().endsWith("/")) {
			cmsSite.setDomain(cmsSite.getDomain().substring(0, cmsSite.getDomain().length() - 2));
		}
		if (StringUtils.isBlank(cmsSite.getTitle())) {
			return NixResult.build(400, "站点名称不能为空！");
		}
		// 重复验证
		CmsSiteExample cmsSiteExample = new CmsSiteExample();
		Criteria criteria = cmsSiteExample.createCriteria();
		criteria.andDomainEqualTo(cmsSite.getDomain());
		criteria.andIdNotEqualTo(cmsSite.getId());
		List<CmsSite> cmsSiteList = cmsSiteMapper.selectByExample(cmsSiteExample);
		if (cmsSiteList != null && cmsSiteList.size() > 0) {
			return NixResult.build(400, "域名已存在！");
		}
		// 更新
		cmsSiteMapper.updateByPrimaryKeySelective(cmsSite);
		return NixResult.ok();
	}

}
